<template>
    <div>
        <Row :gutter="10">
            <Col span="24">
            <Card class="slideList">
                <p slot="title" style="height: 32px">
                    <Icon type="ios-albums-outline"></Icon>
                    版本记录表
                    <Button type="success" @click="addVersion">发布新版本</Button>
                </p>
                <div class="edittable-table-height-con">
                    <Table :columns="slideTableHead" :data="versionList" border :no-filtered-data-text="tipeNews" :no-data-text="tipeNews" ref="slideTableHead"></Table>
                </div>
            </Card>
            </Col>
        </Row>
        <Page :total="total" :current="page" :page-size="pageSize" @on-change="onCurrentChange" style="text-align: center"></Page>
        <Modal v-model="versionVatity" title="等级设置" @on-ok="handleSubmit('versionModel')" @on-cancel="handleReset('versionModel')">
            <Form ref="versionModel" :model="versionModel" :rules="ruleValidate" :label-width="100">
                <FormItem label="版本号" prop="vNumber">
                    <InputNumber :max="1000" :min="1" v-model="versionModel.vNumber"  placeholder="请输入版本号"></InputNumber>
                </FormItem>
                <FormItem label="标题" prop="title">
                    <Input v-model="versionModel.title" placeholder="请输入标题" />
                </FormItem>
                <FormItem label="版本编号" prop="vCode">
                    <Input v-model="versionModel.vCode" placeholder="请输入版本编号" />
                </FormItem>
                <FormItem label="强制更新" prop="group">
                    <Select v-model="versionModel.isForce" style="width:100px">
                        <Option v-for="item in isForceList" :value="item.value" :key="item.value">{{ item.label }}</Option>
                    </Select>
                </FormItem>
                <FormItem label="发布内容" prop="content">
                    <Input v-model="versionModel.content" type="textarea" :autosize="{minRows: 2,maxRows: 7}" placeholder="请输入发布的内容"></Input>
                </FormItem>
            </Form>
        </Modal>
    </div>
</template>
<script>
    export default {
        data () {
            return {
                //表头
                slideTableHead:[
                    {
                        title: '排序',
                        align: 'center',
                        key: 'id',
                        "sortable": true,
                    },
                    {
                        title: '版本号',
                        align: 'center',
                        key: 'vNumber',
                    },
                    {
                        title: '标题',
                        align: 'center',
                        key: 'title',
                    },
                    {
                        title: '版本编号',
                        align: 'center',
                        key: 'vCode',
                    },
                    {
                        title: '更新内容',
                        align: 'center',
                        width:'500',
                        key: 'content',
                    },
                    {
                        title:'强制更新',
                        key:'isForce',
                        align:'center',
                        filters: [
                            {label: '否',value: 0},
                            {label: '是',value: 1}

                        ],
                        filterMultiple:false,
                        filterMethod (v,r){
                            if (v === 0) {
                                return r.isForce === 0;
                            } else if (v === 1) {
                                return r.isForce === 1;
                            }
                        },
                        render(h,column,index){
                            const row=column.row;
                            if(row.isForce==0){
                                return h('Icon', {
                                    props: {
                                        type:"checkmark",
                                        color:'#009966'
                                    }
                                });
                            }else if(row.isForce==1){
                                return h('Icon', {
                                    props: {
                                        type:"android-close",
                                        color:'#990000'
                                    }
                                });
                            }
                        }
                    },
                    {
                        title: '发布时间',
                        align: 'center',
                        key: 'time',
                    },


                ],
                selectURl:'/version/selectVersion',
                upOrAddURl:'/version/addVersion',
                versionList:[],
                tipeNews:'暂无数据',
                versionVatity:false,
                versionModel:{},
                isForceList:[
                {
                    value: 0,
                    label: '否'
                },
                {
                    value: 1,
                    label: '是'
                }
            ],
                ruleValidate: {
                    title: [
                        { required: true, message: '标题内容不能为空', trigger: 'blur' },
                    ],
                    vCode: [
                        { required: true, message: '版本编号内容不能为空', trigger: 'blur' },
                    ],
                    content: [
                        { required: true, message: '发布内容内容不能为空', trigger: 'blur' },
                    ]
                },
                total:0,
                pageSize:0,
                page:1,
            }
        },
        created () {
            this.getData();
        },
        methods: {
            //初始化时获取数据
            getData () {
                let self = this;
                self.$axios.get(self.selectURl + '/' + self.page).then((res) => {
                    for (var i = 0; i < res.data.data.rows.length; i++) {
                        let time =  new Date(parseInt(("/Date("+res.data.data.rows[i].time+")/").substr(6, 13))).toLocaleDateString();
                        res.data.data.rows[i].time = time;
                    }
                    self.versionList = res.data.data.rows;
                    self.page = res.data.data.page;
                    self.pageSize = res.data.data.pagesize;
                    self.total = res.data.data.total;
                })
            },
            addVersion(){
                let self = this;
                self.versionModel={};
                self.versionVatity=true;
            },
            handleSubmit(){
                let self=this;
                self.versionVatity=false;
                debugger;
                self.$axios.post(self.upOrAddURl,self.versionModel).then((res) => {
                    if (res.data.code==200){
                        self.$Message.success('恭喜您，添加成功');
                        self.getData();
                    }else {
                        self.$Message.error('抱歉，添加失败。请稍后重试!');
                        self.getData();
                    }
                })
            },
            handleReset(){
                this.versionVatity=false;
            },
            onCurrentChange(val){
                let self = this;
                self.page = val;
                self.getData();
            }

        }
    }
</script>